def find_time_index(data, name, good_times):
    """ Assumes that only one sample at each time stamp
    Return index vector where the good_times are in data
    """
    # Sort it for speed improvement later
    good_times = sorted(good_times)

    idxlist = []
    idx = 0
    lidx = 0

    for gtime in data[:][name]:
        time_found = False
        for good_time in good_times[lidx::]:
            if gtime == good_time:
                # Found one sample at the specified time and since there only is one at each time go to next time.
                time_found = True
                lidx = lidx + 1
                break
        if time_found:
            idxlist.append(idx)
        idx = idx + 1
    return idxlist

def find_time_index_fast(data, name, good_times):
    """ Assumes that only one sample at each time stamp
    Return index vector where the good_times are in data
    """
    # Sort it for speed improvement later
    good_times = sorted(good_times)

    idxlist = []
    lidx = 0
    idx = 0
    for good_time in good_times:
        for gtime in data[lidx::][name]:
            if gtime == good_time:
                lidx = idx
                break
            idx = idx + 1
        idxlist.append(lidx)
    return idxlist

def find_good_times(data, info):
    """ Find times where all measurements are represented
    Return list of all the good time stamps
    """
    name_list = info.keys()
    max_sample = max(info.values())
    # Find the time stamps that are represented in all measurements
    for i in range(len(name_list)):
        name = name_list[i] + 'Time'
        if i == 0:
            combined = set(data[:][name])
        else:
            combined = combined.intersection(set(data[:][name]))
    # Info to the user
    num_of_usefull_sample = int(len(combined))
    diff = int(max_sample) - int(num_of_usefull_sample)
    print "Num of bad samples:" + str(diff)
    print "Num of usefull samples:" + str(num_of_usefull_sample)
    return list(combined)

def construct_good_data(data, info, good_times):
    """ Find data at specified time stamps
    Return a new data structure with samples only at the specified time stamps
    """
    name_list = info.keys()
    good_data = []
    for (i, name) in enumerate(name_list):
        timename = name + "Time"
        # This is what might take a long time
        idxlist = find_time_index_fast(data, timename, good_times)
        if i == 0:
            time = data[idxlist][timename]
        good_data.append(data[idxlist][name])
    good_data.insert(0,time)
    return good_data

def remove_type_from_data_set(info, info_str, types_to_remove):
    """ Remove one specified type from the data set by changing the what to load in from the text file
    return a new list
    """
    use_cols = []
    bad_cols = []
    for (i, string) in enumerate(info_str):
        if string == types_to_remove:
            bad_cols.append(i)
        else:
            use_cols.append(i)
    for bad in bad_cols:
        use_cols.pop(bad)
    try:
        del info[types_to_remove]
    except:
        pass
    return use_cols, info
